﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    /// <summary>
    /// 滚动控件
    /// </summary>
    public partial class ScrollControl : System.Windows.Forms.UserControl
    {
        /// <summary>
        /// 滚动图的扩展支持。
        /// </summary>
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        internal protected ScrollableControlExtension scrollableControlExtension;
        private Color borderColor = Color.Gainsboro;

        /// <summary>
        /// 初始化 Marmat.Forms.Skin.ScrollControl 类的新实例。
        /// </summary>
        public ScrollControl()
        {
            this.scrollableControlExtension = new ScrollableControlExtension(this);
            this.scrollableControlExtension.BorderColor = this.borderColor;
        }

        /// <summary>
        /// 获取或设置用户控件的边框样式。
        /// </summary>
        public new BorderStyle BorderStyle
        {
            get => base.BorderStyle;
            set
            {
                base.BorderStyle = value;
                this.scrollableControlExtension.BorderStyle = value;
                this.UpdateStyles();
            }
        }

        /// <summary>
        /// 获取或设置控件的边框颜色
        /// </summary>
        [Category("外观")]
        [DefaultValue(typeof(Color), "Gainsboro")]
        public Color BorderColor
        {
            get => borderColor;
            set
            {
                borderColor = value;
                this.scrollableControlExtension.BorderColor = value;
            }
        }
        /// <summary>
        /// 设置滚动条的大小。
        /// </summary>
        /// <param name="width">滚动条的宽度/高度。</param>
        public void SetScrollSize(int width)
        {
            this.scrollableControlExtension.SetScrollBorder(width + 5);
            this.scrollableControlExtension.SetScrollSize(width);
        }

        /// <summary>
        /// 处理windows消息
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            if (this.scrollableControlExtension.BeforeWndProc(ref m, out var arguments))
                base.WndProc(ref m);
            this.scrollableControlExtension.AfterWndProc(m, arguments);
        }

        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            this.Invalidate();
        }

        protected override void OnScroll(ScrollEventArgs se)
        {
            base.OnScroll(se);
            this.Invalidate();
        }
    }
}
